/*
 * To change this license header, choose License Headers in Project Properties.
 * To change this template file, choose Tools | Templates
 * and open the template in the editor.
 */
package test;

import java.lang.reflect.Field;

/**
 *
 * @author Khuong Dao
 */
public class Generator {

    private String pkg;
    private Class cls;

    private Generator(Class c, String s) {
        pkg = s;
        cls = c;
    }

    public Generator() {
    }

    public static Generator getGenerator(Class className, String modelPackage) {
        Generator ret = new Generator(className, modelPackage);
        // Generate DTO
//        ret.generateDTO();
        // Generate mapper
        ret.generateMapper();
//        // Generate Service
//        generateServices();
//        // Generate Repository
//        generateRepository();
        return ret;
    }

    private void generateDTO() {
        StringBuilder out = new StringBuilder();
        System.out.println("// Package DTO");
        System.out.println("package " + pkg + ".dto;");
        System.out.println("import java.io.Serializable;");
        System.out.println("public class " + cls.getSimpleName() + "DTO  implements Serializable {");
        Field fls[] = cls.getDeclaredFields();
        if (fls != null) {
            for (Field f : fls) {
                String type = f.getType().getName();
                System.out.println("    private " + type + " " + f.getName() + ";");
                // Getter
                String postFix = upperFirstLetter(f.getName());
                System.out.println("    public " + type + " get" + postFix + "(){");
                System.out.println("        return this." + f.getName() + ";");
                System.out.println("    }");
                // setter
                System.out.println("    public void set" + postFix + "(" + type + " tmp){");
                System.out.println("        this." + f.getName() + " = tmp;");
                System.out.println("    }");
            }
        }
        System.out.println("    public " + cls.getSimpleName() + "DTO (){}");
        System.out.println("    ");
        System.out.println("}    ");
        System.out.println("");
    }

    private void generateMapper() {
        System.out.println("// Package DTO");
        System.out.println("package " + pkg + ".mapper;");
        System.out.println("import java.io.Serializable;");
        System.out.println("import " + pkg + ".model.*;");
        System.out.println("import " + pkg + ".dto.*;");
        System.out.println("public class " + cls.getSimpleName() + "Mapper  implements Serializable {");
        System.out.println("    public " + cls.getSimpleName() + " dtoToPersistence(" + cls.getSimpleName() + "DTO dto){");
        System.out.println("        if(dto == null){");
        System.out.println("           return null;");
        System.out.println("        }");
        System.out.println("        " + cls.getSimpleName() + " ret = new " + cls.getSimpleName() + "();");
        Field fls[] = cls.getDeclaredFields();
        if (fls != null) {
            for (Field f : fls) {
                String type = f.getType().getName();
                String postFix = upperFirstLetter(f.getName());
                if (postFix.contains("erialVersionUID")) {
                    continue;
                }
                System.out.println("        ret.set" + postFix + "(dto.get" + postFix + "());");
            }
        }
        System.out.println("        return ret;");
        System.out.println("    }");

        System.out.println("    public " + cls.getSimpleName() +"DTO persistentToDto(" + cls.getSimpleName() + " model){");
        System.out.println("        if(model == null){");
        System.out.println("           return null;");
        System.out.println("        }");
        System.out.println("        " + cls.getSimpleName() + "DTO ret = new " + cls.getSimpleName() + "DTO();");
        fls = cls.getDeclaredFields();
        if (fls != null) {
            for (Field f : fls) {
                String type = f.getType().getName();
                String postFix = upperFirstLetter(f.getName());
                if (postFix.contains("erialVersionUID")) {
                    continue;
                }
                System.out.println("        ret.set" + postFix + "(model.get" + postFix + "());");
            }
        }
        System.out.println("        return ret;");
        System.out.println("    }");

        System.out.println("    public " + cls.getSimpleName() + "Mapper (){}");
        System.out.println("    ");
        System.out.println("}    ");
        System.out.println("");
    }

    private String upperFirstLetter(String name) {
        if (name == null) {
            return "";
        }
        StringBuilder b = new StringBuilder();
        for (int i = 0; i < name.length(); i++) {
            String c = name.charAt(i) + "";
            if (i == 0) {
                c = c.toUpperCase();
            }
            b.append(c);
        }
        return b.toString();
    }
}
